﻿/*
 * @Autor: PK
 * @Date: 2022/01/25 20:39:SS
 */

using System;
using System.Collections.Generic;
using System.Data;
using ShkSimulation.core.dao.output.result;
using ShkSimulation.core.exceptions;

namespace ShkSimulation.core.optimization.extractor.result {

	public class DefaultTargetVariableExtractor: TargetVariableExtractor {

		private readonly NamedCellPointGroup pointGroup;

		public DefaultTargetVariableExtractor(NamedCellPointGroup pointGroup) {
			this.pointGroup = pointGroup;
		}

		public Dictionary<string, double> ExtractNamedDecisionVariables(Result result) {
			DataTableResourceResult resourceResult = new DefaultDataTableResourceResult(result);
			DataTable data = resourceResult.GetDataTable();
			Dictionary<string, double> resultDictionary = new Dictionary<string, double>();
			foreach (Dictionary<string, CellPoint> cellPoints in pointGroup.location.Values) {
				foreach ((string name, CellPoint point) in cellPoints) {
					if (point.Row >= data.Rows.Count || point.Column >= data.Columns.Count) {
						throw new ErrorParamException("属性提取器超过了索引：" + name);
					}
					resultDictionary.Add(name, Convert.ToDouble(data.Rows[point.Row][point.Column]));
				}
			}
			return resultDictionary;
		}

	}

}